package com.hd.until;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;


public class LogFilterUtil {
	
	static Map<String,  MaskPosition> MASK_WORDS_SUFFIX_MAP = new HashMap<String, MaskPosition>();
	
	static {
	    MASK_WORDS_SUFFIX_MAP.put("SALARY", new MaskPosition(0, 0));
	    MASK_WORDS_SUFFIX_MAP.put("MONTHSALARY", new MaskPosition(0, 0));
	    MASK_WORDS_SUFFIX_MAP.put("MONTHINCOME", new MaskPosition(0, 0));
		MASK_WORDS_SUFFIX_MAP.put("PASSWORD", new MaskPosition(0, 0));
	    MASK_WORDS_SUFFIX_MAP.put("BYKEK", new MaskPosition(0, 0));
	    MASK_WORDS_SUFFIX_MAP.put("BYTPK", new MaskPosition(0, 0));
	    
	    MASK_WORDS_SUFFIX_MAP.put("PHONENUMBER", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("MOBILE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("MOBILEPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("FIRSTCONTACTPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("SECONDCONTACTPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("BINDMOBILEPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("BANKCARDPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("COMACCOUNTPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CONTACTMOBILE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("PHONENO", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("MEMBERPHONE", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CONTACTNUMBER", new MaskPosition(3, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CELLPHONENUMBER", new MaskPosition(3, 4));

	    MASK_WORDS_SUFFIX_MAP.put("IDCARDNUMBER", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CERTID", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CONTACTID", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("IDNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CARDNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("IDCARD", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("ACCNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("IDCARDNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CERTIFICATENUMBER", new MaskPosition(6, 4));

	    MASK_WORDS_SUFFIX_MAP.put("USERNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("REALNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("FULLNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("IDHOLDER", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("FIRSTCONTACTNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("SECONDCONTACTNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("BANKACCOUNTNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("MEMBERNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("PLATFORMUSERNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("CONTACTNAME", new MaskPosition(1, 0));
	    MASK_WORDS_SUFFIX_MAP.put("COMACCOUNTNAME", new MaskPosition(1, 0));

	    MASK_WORDS_SUFFIX_MAP.put("HTMLSTRING", new MaskPosition(0, 0));
	    
	    MASK_WORDS_SUFFIX_MAP.put("BANKCARDNUMBER", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("COMACCOUNTNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("BANKCARDNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("BANKACCOUNTNO", new MaskPosition(6, 4));
	    MASK_WORDS_SUFFIX_MAP.put("CASHOUTACCOUNTNO", new MaskPosition(6, 4));


	}
	
	private static Object maskWordsSuffix(String fieldName, Object fieldValue)  {
		if (fieldValue == null){
			return null;
		}
		
		MaskPosition maskPosition = MASK_WORDS_SUFFIX_MAP.get(fieldName.toUpperCase());
		if (maskPosition != null) {
			try {
				String fileValueStr = fieldValue.toString();
				return cutSuffixStr(fileValueStr, maskPosition.getFront(), maskPosition.getEnd());
			} catch (Exception e) {
				return fieldValue;
			}
		}
		return fieldValue;
	}
	
	private static String  cutSuffixStr(String fieldValue,int start,int end) {
        if (StringUtils.isBlank(fieldValue)) {
            return "";
        }
        StringBuffer sb = new StringBuffer(fieldValue);
        int length = sb.length();
        String mask = getReplacedString(length - start - end, '*');
        sb.replace(start, length - end, mask);
        return sb.toString();
    }
    
    private static String getReplacedString(int length, char ch) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append(ch);
        }
        return sb.toString();
    }
    
	public static String toFilteredString(Object object) {
	    return new ReflectionToStringBuilder(object) {
	    	protected Object getValue(Field field)
	    		    throws IllegalArgumentException, IllegalAccessException
	    		  {
	    				return maskWordsSuffix(field.getName(), field.get(getObject()));
	    		  }
	    }.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(cutSuffixStr("1212", 3,4));
	}

}
